package com.sphd.Restaurant.fragments.cadrsandcouponsFragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.activity.CoupinsDetailsActivity;
import com.sphd.Restaurant.base.LazyLoadFragment;
import com.sphd.Restaurant.infos.CouponInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.CouponsP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.CouponsView;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2015/12/2.
 * 优惠券 fragment
 */
public class CouponsFragment extends LazyLoadFragment implements CouponsView {

    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    BaseQuickAdapter<CouponInfo> baseQuickAdapter;
    List<CouponInfo> couponInfos;
    CouponsP couponsP;
    int curpage = 1;

    @Override
    public int getLayout() {
        return R.layout.fragment_coupons_cards;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        initView();
    }

    @Override
    public void loadData() {
        if (getBoolean(Constants.ISLOGIN))
            addData();
    }


    private void initView() {
        setRefresh(mySrf);
        setRecycle(myRecy);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        baseQuickAdapter.setOnLoadMoreListener(() -> {
            curpage++;
            addData();
        });


          /*点击事件*/
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle =new Bundle();
                bundle.putSerializable(Constants.COUPONS, (CouponInfo) baseQuickAdapter.getItem(position));
                startActivity(CoupinsDetailsActivity.class,bundle);
            }
        });

        mySrf.setOnRefreshListener(() -> {
            if (getBoolean(Constants.ISLOGIN)) {
                curpage = 1;
                addData();
            } else {
                mySrf.setRefreshing(false);
            }
        });
    }

    private void initAdapter() {
        couponsP = new CouponsP(this);
        couponInfos = new ArrayList<>();
        baseQuickAdapter = new BaseQuickAdapter<CouponInfo>(R.layout.item_fragment_cardsandcoupons, couponInfos) {
            @Override
            protected void convert(BaseViewHolder helper, CouponInfo item) {

                helper.setText(R.id.tv_most_lift, item.getCard_cgname())
                        .setText(R.id.tv_most_right, "您在该店有" + item.getCount() + "张优惠券");
            }
        };
        baseQuickAdapter.openLoadAnimation();
        baseQuickAdapter.openLoadMore(10);
        myRecy.setAdapter(baseQuickAdapter);
    }
    //// TODO: 2016/10/20
    private void addData() {
        couponsP.addData(Model.HTTPURL + Model.COUPONS + "username="+getString(Constants.USERPHONE)+"&curpage=" + curpage +
                "&pageno=10");
    }


    @Override
    public void sendCoupList(List<CouponInfo> couponList) {
        if (curpage == 1) {
            baseQuickAdapter.setNewData(couponList);
        } else {
            baseQuickAdapter.addData(couponList);
        }
        if (couponList.size() != 10) {
            addFootView(baseQuickAdapter);
        }
        setLoadDataCompleted(true);
    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        mySrf.setRefreshing(false);
        dissMissDialog();
    }


}
